package com.seraph.td.socket;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

import weibo4andriod.org.json.JSONException;
import weibo4andriod.org.json.JSONObject;

public class UuseeRemoteTest implements UuseeRemoteServerListener {

	/**
	 * @param args
	 */
	private static final int SCAN_PORT = 5752;
	static final String head ="UUSEE_REMOVE_CMD_BEGIN";
	static final String tail ="UUSEE_REMOVE_CMD_END";
	static final int port = 65534;

	public void recvUDP(){
		byte data[] = new byte[1024];
		DatagramSocket rDatagramSocket;
		try{
			while(true){
				rDatagramSocket = new DatagramSocket(SCAN_PORT);
				DatagramPacket pack = new DatagramPacket(data, data.length);
				System.out.println("WAIT RECEIVE PACK");
				rDatagramSocket.receive(pack);
				System.out.println("RECEIVE ONE PACK");
				rDatagramSocket.close();

			}
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		
		
	}
	
	
	public void sendUDP(){
		

		// TODO Auto-generated method stub
		DatagramSocket mDatagramSocket;
		try{
			String SCAN_CMD="seraph--------------------END";
			byte[] data = SCAN_CMD.getBytes();
			InetSocketAddress addr = new InetSocketAddress("255.255.255.255",SCAN_PORT);
//			InetSocketAddress addr = new InetSocketAddress("192.168.1.110",SCAN_PORT);
			mDatagramSocket = new DatagramSocket();
			DatagramPacket pack = new DatagramPacket(data, data.length,addr);
			mDatagramSocket.send(pack);
			
			System.out.println("send one  DatagramSocket");
//			byte buf[] = new  byte[1024];
//			DatagramPacket pack2 = new DatagramPacket(buf, buf.length);
//			System.out.println("wait  recive  one pack");
//			mDatagramSocket.receive(pack2);
//			
//			System.out.println(new String(buf,0,pack2.getLength()));
			mDatagramSocket.close();
		}catch (IOException e) {
			// TODO: handle exception
//			System.out.println(e.toString());
			e.printStackTrace();
		} 
		
	
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UuseeRemoteTest sulf =new  UuseeRemoteTest();

		
		
//		UuseeRemoteServer server = UuseeRemoteServer.open(port, sulf);
	}

	private Thread workThrad = new Thread(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				BufferedReader readerCMD = new BufferedReader( new InputStreamReader(System.in) );
				String str =readerCMD.readLine();
//				
				int cmd = Integer.valueOf(str);
				
				while(cmd!=-1){
					System.out.println(cmd);
					switch(cmd){
					case 1:
						sendCMD(new UuseeRemoveCMD.PLAY().toString());
						break;
					case 2:
						sendCMD(new UuseeRemoveCMD.PASH().toString());
						break;
					case 3:
						sendCMD(new UuseeRemoveCMD.SPEED().toString());
						break;
					case 4:
						sendCMD(new UuseeRemoveCMD.BACK().toString());
						break;
					case 5 :
						sendUDP();
						break;
					default :
						break;
					}
					str =readerCMD.readLine();
					cmd = Integer.valueOf(str);
					
				}
				
				sSocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		
	};
	Socket sSocket;
	SocketAddress endpoint;// = new InetSocketAddress("127.0.0.1",port);
	InputStream in = null ;
	OutputStream out = null;
	BufferedReader readerNET =null;// new BufferedReader( new InputStreamReader(in) );
	public void sendCMD(String cmd){

		// TODO Auto-generated method stub
		try {
			
				StringBuilder sb = new StringBuilder();
				JSONObject obj = new JSONObject();
				System.out.println(cmd);
				sb.append(head);
				sb.append('\n');
				sb.append(cmd);
				sb.append('\n');
				sb.append(tail);
				sb.append('\n');
				out.write(sb.toString().getBytes());
				String requset = readerNET.readLine();
				System.out.println(requset);
//			mSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if(in !=null){
				try {
					in.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			if(out !=null){
				try {
					out.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			if(sSocket !=null && !sSocket.isClosed()){
				try {
					sSocket.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
				
		}finally{
			
		}
	
	}
	
	
	
	
	
	public UuseeRemoteTest() {
		sSocket = new Socket();
		try {
			endpoint = new InetSocketAddress("127.0.0.1",port);
			sSocket.connect(endpoint);
			in = sSocket.getInputStream() ;
			out = sSocket.getOutputStream();
			readerNET =new BufferedReader(new InputStreamReader(in));
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		workThrad.start();
	
		// TODO Auto-generated constructor stub
	}


	@Override
	public void onConnect(String ip, int port) {
		// TODO Auto-generated method stub
		System.out.println("begin");
		System.out.println("host====="+ip);
		System.out.println("port===="+port);
	}

	@Override
	public void onReceive(String data) {
		// TODO Auto-generated method stub
		System.out.println("onReceive------------data===="+data);
	}

	@Override
	public void onClosed() {
		// TODO Auto-generated method stub
		System.out.println("close");
	}

}
